<template>
  <div class="app-container">
    <el-button type="primary" @click="showDialog(false)">添加一个分类</el-button>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%; margin-top: 30px"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.categoryId }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="分类">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="简介">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="showDialog(true, scope.row)">修改</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteCategory(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-dialog v-el-drag-dialog :visible.sync="dialog.visible" :title="dialog.title">
      <el-form ref="ruleForm" :model="dialog.form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="分类名" prop="title">
          <el-input v-model="dialog.form.title" />
        </el-form-item>
        <el-form-item label="分类简介" prop="title">
          <el-input v-model="dialog.form.description" type="textarea" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="dialog.loding"
            @click="submitDialog()"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, craeteCategory, updateCategory, deleteCategory } from '@/api/category'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import { MessageBox } from 'element-ui'

export default {
  name: 'CategoryList',
  components: { Pagination },
  directives: { elDragDialog },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 0,
        limit: 10
      },
      dialog: {
        form: this.getDefaultDialogForm(),
        title: '添加分類',
        loding: false,
        visible: false,
        isEdit: false
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    showDialog(isEdit, row) {
      if (isEdit) {
        this.dialog.form = row
        this.dialog.title = '更新分類'
      } else {
        this.dialog.form = this.getDefaultDialogForm()
        this.dialog.title = '添加分類'
      }
      this.dialog.isEdit = isEdit
      this.dialog.visible = true
    },
    submitDialog() {
      this.dialog.loding = true
      if (this.dialog.isEdit) {
        updateCategory(this.dialog.form).then(() => {
          this.dialog.loding = false
          this.dialog.visible = false
        })
      } else {
        craeteCategory(this.dialog.form).then(() => {
          this.dialog.loding = false
          this.dialog.visible = false
          this.getList()
        })
      }
    },
    deleteCategory(row) {
      MessageBox.confirm('确定删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCategory(row.categoryId).then(() => { this.getList() })
      })
    },
    getDefaultDialogForm() {
      return {
        categoryId: null,
        title: null,
        craeteTime: null,
        description: null
      }
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
